<template>
  <div class="ms-list">
    <el-row class="operation">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">
          <icon style="" name="home"></icon>
        </el-breadcrumb-item>
        <el-breadcrumb-item>客户管理</el-breadcrumb-item>
        <el-breadcrumb-item>问卷管理</el-breadcrumb-item>
        <el-breadcrumb-item>有效率排行榜</el-breadcrumb-item>
        <el-breadcrumb-item>列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>

    <div ref="msSearch">
      <!--@clickH="calculatedHeight"-->
      <ms-search :data="search.list" @click="updateConditions"/>
    </div>

    <el-table
      v-loading="table.loading"
      size="small" stripe border
      :data="table.list"

      :fit="true">
      <el-table-column prop="surveyTitle" label="问卷标题" min-width="200">
        <template slot-scope="scope">
          《{{ scope.row.surveyTitle }}》
        </template>
      </el-table-column>
      <el-table-column prop="senderName" label="发放者" min-width="90" fixed></el-table-column>
      <el-table-column prop="fill" label="填写量" min-width="85" align="right" header-align="center"></el-table-column>
      <el-table-column prop="browse" label="浏览量" min-width="85" align="right" header-align="center"></el-table-column>
      <el-table-column prop="effectiveRate" label="有效完成率" min-width="85" align="right" header-align="center">
        <template slot-scope="scope">
          {{scope.row.effectiveRate.toFixed(2)}} %
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="75">
        <template slot-scope="scope">
          <el-badge :value="scope.row.thumbUpCount" :max="99" class="item">
            <el-button type="success" @click="thumbUp(scope.$index)" size="mini" round>
              <icon name="thumbs-up"></icon>
            </el-button>
          </el-badge>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="table.pagination.page"
      :total="table.pagination.totalRow"
      :page-size="table.pagination.pageSize"
      :page-sizes="table.pagination.pageSizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
  import MsSearch from '@/components/common/Search.vue'
  import Api from '@/api/customer/platform/survey-rank'
  import { SEARCH_CONDITION } from '@/models/customer/platform/survey-rank'
  import ResponseParse from '@/utils/response-parse'

  export default {
    components: {
      MsSearch
    },
    data () {
      return {
        search: {
          list: [],
          condition: {}
        },
        table: {
          list: [],
          pagination: {},
          loading: true
        }
//        PHeigh: document.documentElement.clientHeight - 120,
//        tbHeight: 100
      }
    },
//    mounted: function () {
//      this.$nextTick(function () { this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight) })
//    },
    created () {
      this.search.list = SEARCH_CONDITION
      this.table.list = []
      this.requestList()
    },
    methods: {
      searchCriteria (val, index) {
        this.$set(this.search.list[index], 'value', val)
      },
//      calculatedHeight () {
//        this.$nextTick(function () {
//          this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight)
//        })
//      },
      requestList () {
        this.table.loading = true
        const search = Object.assign({}, this.pagination, this.search.condition)
        Api.list({search: search}, response => {
          let status = response.status || 0
          let body = response.data || []
          if (status === 200 || status === 204) {
            this.table.list = body
            this.table.pagination = ResponseParse.pagination(response.headers)
//            if (this.table.pagination.totalPage === this.table.pagination.page) {
//              this.tbHeight = ''
//            } else {
//              this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight)
//            }
          } else {
//            this.tbHeight = ''
            this.table.list = []
            this.$message.error(body.error || '数据获取失败')
          }
          this.table.loading = false
        })
      },
      handleSizeChange (val) {
        this.table.pagination.pageSize = val
        this.requestList()
      },
      handleCurrentChange (val) {
        this.table.pagination.page = val
        this.requestList()
      },
      updateConditions (conditions) {
        this.table.pagination.page = 1
        this.search.condition = conditions
        this.requestList()
      },
      thumbUp (index) {
        const data = this.table.list[index]
        Api.thumb({id: data.id}, response => {
          let status = response.status || 0
          let body = response.data || []
          if (status >= 200 && status <= 204) {
            this.table.list[index].thumbUpCount = body.thumbUpCount
          } else {
            this.$message.error(body.error || '操作失败')
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .item {
    margin-top: 10px;
    margin-right: 40px;
  }
</style>
